<template>
  <teleport to="body">
    <transition name="popule-fade">
      <view v-if="visible" class="popule-mask" @click.self="handleClose">
        <view class="popule-dialog">
          <header class="popule-header">
            <h3>{{ title }}</h3>
            <view class="popule-close" type="button" @click="handleClose"></view>
          </header>
          <section class="popule-body">
            <p v-if="description">{{ description }}</p>
            <!-- 列表模式 -->
            <view v-if="listData && listData.length > 0" class="popule-list">
              <view 
                v-for="(item, index) in listData" 
                :key="getItemKey(item, index)"
                class="popule-list-item"
                :class="{ 'selected': isItemSelected(item, index) }"
                @click="handleItemClick(item, index)"
              >
                <text class="list-item-text">{{ getItemLabel(item, index) }}</text>
                <view v-if="isItemSelected(item, index)" class="list-item-check">✓</view>
              </view>
            </view>
            <!-- 默认插槽内容 -->
            <slot />
          </section>
          <footer v-if="showSubmit" class="popule-footer">
            <view class="popule-cancel" type="button" @click="handleClose">取消</view>
            <view class="popule-submit" type="button" @click="handleSubmit">{{ submitText }}</view>
          </footer>
        </view>
      </view>
    </transition>
  </teleport>
</template>

<script setup lang="ts">
import { computed } from 'vue';

interface Props {
  modelValue: boolean;
  title: string;
  description?: string;
  submitText?: string;
  // 列表相关属性
  listData?: any[]; // 列表数据
  itemLabel?: string | ((item: any, index: number) => string); // 显示字段名或函数
  itemKey?: string | ((item: any, index: number) => string | number); // 唯一标识字段名或函数
  selectedValue?: any; // 当前选中的值
  showSubmit?: boolean; // 是否显示提交按钮（列表模式下通常不需要）
}

const props = withDefaults(defineProps<Props>(), {
  description: '请确认后再提交',
  submitText: '提交',
  listData: () => [],
  showSubmit: true,
});

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void;
  (e: 'submit'): void;
  (e: 'item-click', item: any, index: number): void; // 列表项点击事件
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit('update:modelValue', value),
});

const handleClose = () => {
  visible.value = false;
};

const handleSubmit = () => {
  emit('submit');
};

// 获取列表项的唯一标识
const getItemKey = (item: any, index: number): string | number => {
  if (typeof props.itemKey === 'function') {
    return props.itemKey(item, index);
  }
  if (typeof props.itemKey === 'string') {
    return item[props.itemKey] ?? index;
  }
  return item.id ?? item.key ?? index;
};

// 获取列表项的显示文本
const getItemLabel = (item: any, index: number): string => {
  if (typeof props.itemLabel === 'function') {
    return props.itemLabel(item, index);
  }
  if (typeof props.itemLabel === 'string') {
    return item[props.itemLabel] ?? String(item);
  }
  // 默认尝试常见的字段名
  return item.name ?? item.label ?? item.title ?? item.matchScheduleModelName ?? item.nameTemplate ?? String(item);
};

// 判断列表项是否被选中
const isItemSelected = (item: any, index: number): boolean => {
  if (props.selectedValue === undefined || props.selectedValue === null) {
    return false;
  }
  const itemKey = getItemKey(item, index);
  // 支持直接值比较和ID比较
  return props.selectedValue === itemKey || 
         props.selectedValue === item.id || 
         props.selectedValue === item.matchScheduleModelId ||
         String(props.selectedValue) === String(itemKey);
};

// 处理列表项点击
const handleItemClick = (item: any, index: number) => {
  emit('item-click', item, index);
};
</script>

<style scoped>
.popule-mask {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.45);
  z-index: 999;
}

.popule-dialog {
  width: 420px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.25);
  overflow: hidden;
  border: 4rpx solid #2ADBE4;
}

.popule-header {
  background: #3D85EC;
  border-radius: 8rpx 8rpx 0 0;
  padding: 5rpx 10rpx;
  text-align: center;
  position: relative;
  font-size: 16rpx;
  color: #fff;
}

.popule-close {
  position: absolute;
  right: 10rpx;
  top: 10rpx;
  width: 15rpx;
  height: 15rpx;
  background: url('/static/icon/close.png') no-repeat center center;
							background-size: 100% 100%;
							color: #fff;
}

.popule-body {
  padding: 20px;
  color: #475569;
  line-height: 1.6;
  max-height: 60vh;
  overflow-y: auto;
}

.popule-list {
  margin-top: 10px;
  max-height: 50vh;
  overflow-y: auto;
}

.popule-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  margin-bottom: 8px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: #fff;
}

.popule-list-item:hover {
  background: #f8fafc;
  border-color: #3D85EC;
}

.popule-list-item.selected {
  background: #eff6ff;
  border-color: #3D85EC;
  color: #3D85EC;
}

.list-item-text {
  flex: 1;
  font-size: 14px;
  color: #475569;
}

.popule-list-item.selected .list-item-text {
  color: #3D85EC;
  font-weight: 500;
}

.list-item-check {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #3D85EC;
  font-weight: bold;
  font-size: 16px;
}

.popule-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 8px 20px 10px;
  border-top: 1px solid #f1f5f9;
}

.popule-cancel,
.popule-submit {
  /* min-width: 92px; */
  padding: 4px 16px;
  text-align: center;font-size: 12rpx;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s ease;
}

.popule-cancel {
  border-color: #cbd5f5;
  color: #475569;
  background: #fff;
}

.popule-cancel:hover {
  background: #f8fafc;
}

.popule-submit {
  background: #2563eb;
  color: #fff;
}

.popule-submit:hover {
  background: #1e40af;
}

.popule-fade-enter-active,
.popule-fade-leave-active {
  transition: opacity 0.25s ease;
}

.popule-fade-enter-from,
.popule-fade-leave-to {
  opacity: 0;
}
</style>

<!--
使用示例：
<template>
  <button @click="showDialog = true">打开弹窗</button>
  <PopuleView
    v-model="showDialog"
    title="删除确认"
    description="该操作无法撤销，请确认是否要删除这条记录。"
    submit-text="确认删除"
    @submit="handleDelete"
  >
    <p>你也可以通过默认 slot 传入更多自定义内容。</p>
  </PopuleView>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PopuleView from '@/components/UiView/PopuleView.vue';

const showDialog = ref(false);
const handleDelete = () => {
  // 在此处理提交逻辑
  showDialog.value = false;
};
</script>
-->
